package com.app.bimo.library_db.helper;

import com.app.bimo.library_db.ReadTimeRecord;
import com.app.bimo.library_db.UserData;
import com.app.bimo.library_db.dao.DaoSession;
import com.app.bimo.library_db.dao.ReadTimeRecordDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

public class ReadTimeRecordHelper {


    private static volatile ReadTimeRecordHelper sInstance;
    private static DaoSession daoSession;
    private static ReadTimeRecordDao recordDao;

    public static ReadTimeRecordHelper getsInstance() {
        if (sInstance == null) {
            synchronized (UserHelper.class) {
                if (sInstance == null) {
                    sInstance = new ReadTimeRecordHelper();
                    daoSession = DaoDbHelper.getInstance().getSession();
                    recordDao = daoSession.getReadTimeRecordDao();
                }
            }
        }
        return sInstance;
    }

    /**
     * 保存阅读数据
     *
     * @param readTimeRecord
     */
    public void saveRead(ReadTimeRecord readTimeRecord) {
        recordDao.insertOrReplace(readTimeRecord);
        recordDao.detach(readTimeRecord);
    }

    /**
     * 保存阅读数据
     *
     * @param readTimeRecord
     */
    public void updateRead(ReadTimeRecord readTimeRecord) {
        recordDao.update(readTimeRecord);
    }

    /**
     * 删除全部数据   用于退出登录时候
     */
    public void removeRecord() {
        recordDao.deleteAll();
    }

    /**
     * 移除已上报数据
     * *
     * 状态  0 未上报  1已上报   2 成功上报
     */
    public void removeUsdRecord() {
        List<ReadTimeRecord> list = recordDao.queryBuilder().where(ReadTimeRecordDao.Properties.Status.eq(2)).list();
        recordDao.deleteInTx(list);
        list = recordDao.queryBuilder().where(ReadTimeRecordDao.Properties.Chapter.eq("")).list();
        recordDao.deleteInTx(list);
    }

    /**
     * 移除已上报数据
     * *
     * 状态  0 未上报  1已上报   2 成功上报
     */
    public void removeUsdRecord(ReadTimeRecord readTimeRecord) {
        recordDao.delete(readTimeRecord);
    }

    /**
     * 改变数据状态
     * *
     * 状态  0 未上报  1已上报   2 成功上报
     */
    public void changeRecordStatus(int status, ReadTimeRecord data) {
        if (status == 2) {
            recordDao.delete(data);
        } else {
            data.setStatus(status);
            recordDao.update(data);
        }
    }


    /**
     * 查询本小说  所有未上报成功的数据
     *
     * @param noverid
     * @return
     */
    public List<ReadTimeRecord> findUnUseRecord(String noverid) {
        UserData data = UserHelper.getsInstance().findUser();
        if (data == null) {
            return new ArrayList<>();
        } else {
            QueryBuilder<ReadTimeRecord> qb = recordDao.queryBuilder().where(ReadTimeRecordDao.Properties.Novelid.eq(noverid), ReadTimeRecordDao.Properties.Status.eq(0), ReadTimeRecordDao.Properties.Uuid.eq(data.getUuid()));
            List<ReadTimeRecord> list = new ArrayList<>();
            List<ReadTimeRecord> qbList = qb.list();
            for (int i = 0; i < qbList.size(); i++) {
                if (qbList.get(i).getChapter().size() > 0) {
                    list.add(qbList.get(i));
                }
            }
            return list;
        }
    }


}
